﻿@page "/template/create"
@attribute [Authorize(Roles = "User, Administrator")]
@inherits OwningComponentBase<ICovenantService>

@using Microsoft.JSInterop
@using Covenant.Core
@using Covenant.Models.Grunts
@inject IJSRuntime IJSRuntime
@inject NavigationManager NavigationManager

<div class="d-flex justify-content-between flex-wrap flex-md-nowrap pb-2 mb-3">
    <h1 class="h2">Create Implant Template</h1>
</div>

<ImplantTemplateForm ImplantTemplate="ImplantTemplate" OnSubmit="OnCreate" SubmitIcon="plus" SubmitLabel="Create" />

@code {
    public ImplantTemplate ImplantTemplate { get; set; } = new ImplantTemplate();

    private async Task OnCreate(ImplantTemplate template)
    {
        try
        {
            await Service.CreateImplantTemplate(template);
            NavigationManager.NavigateTo("/template");
        }
        catch (Exception e) when (e is ControllerNotFoundException || e is ControllerBadRequestException || e is ControllerUnauthorizedException)
        {
            // return RedirectToAction(nameof(Index));
        }
    }
}
